<template>
	<uni-nav-bar title="订单详情" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
	<view class="package center" v-if="orderAll.status!=1">
		未支付，请在
		<uni-section type="line" padding>
			<uni-countdown :second="orderAll.cha" :show-day="false" color="#FF0000" splitorColor="#FF0000" :font-size="16" @timeup="timeup"/>
		</uni-section>
		内完成支付
	</view>
	<view class="package">{{orderAll.title}}</view>
	<view class="package" style="padding:20rpx 0px">
		<view v-for="(item,index) in orderAll.goods_list" :key='item.id'>
			<view class="packageLsit" @click="navTo('shopp/Shopkeeper?id='+item.store_id)">
				<image src="../../static/groupBenefits/shop.png" mode=""></image>
				<text>{{item.store_name}}</text>
				<uni-icons type="forward" size="19" color="#666"></uni-icons>
			</view>
			<ShopList :type="'num'" :shoppAll="item"></ShopList>
		</view>
		<Shoppay :empty="true" :payFor="orderAll" :state="'groupDetail'"></Shoppay>
	</view>
	<PayTime :payFor="orderAll" :state="'team'"></PayTime>
	<ShopBot :title="title"  @submit="submit" :empty="true"></ShopBot>
	<!-- 新建团惠包弹框 -->
	<uni-popup ref="PobulicePopup" type="center" :is-mask-click="false">
	 <pobulice :title="'温馨提示'" :text="text" @close="closeBankPad" @submit="BuyAgain" :type="'center'"></pobulice>
	</uni-popup>
</template>

<script setup lang="ts">
import { navBack, navTo, redirectTo } from '@/utils/navigator';
import ShopList from '@c/groupBenefits/shopGrid.vue';
import Shoppay from '@c/groupBenefits/pay.vue';
import ShopBot from '@c/groupBenefits/shopBot.vue';
import pobulice from '@c/pobulic/public.vue';
import PayTime from '@c/groupBenefits/payTime.vue';
import { errorToast, successToast, showLoading } from '@/utils/prompt';
import { numsDetails } from '@/gql/group';
import { toPublish } from '@mqtt';
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue';
import { getDriverID } from '@/stores/driverID';
/**
 * 团惠包订单详情
 * @param user_id  用户ID
 * @param orderID  订单ID
 * @param orderAll  订单全部信息
 * @param PobulicePopup  团惠包弹框
 * @param text  团惠包弹框提示文字
 * @param title  支付底部文字调整
 * @param empty  底部支付状态
 */
const user_id = getDriverID();
const orderID = ref('');
const orderAll = ref({});

const PobulicePopup = ref(null);
const text = '该团惠包商品中有过期订单 您是否还要重新购买';

const title = ref('');
const empty = ref(true);
const repeat = ref(true); //放重复点击
onLoad((pearm) => {
  orderID.value = pearm.id;
  init();
});
/**
 * 初始化订单信息
 * @param order_id  订单ID
 */
function init() {
  const payload = {
    query: numsDetails,
    variables: {
      order_id: Number(orderID.value),
    },
  };
  toPublish('ql/store/getTeamOrderInfo', payload, (obj: any) => {
    const { team_order_info } = obj.data;
    orderAll.value = team_order_info;
    title.value = team_order_info.status == 1 ? '再来一单' : '继续支付';
    console.log(team_order_info);
    if (team_order_info.status != '1' && team_order_info.cha == 0) {
      empty.value = false;
    }
  });
}
/**
 * 提交订单
 * @param user_id  用户ID
 * @param bao_id  团惠包ID
 * @param fx_user_id  分享人ID
 * @param team_id  团ID
 */
function submit() {
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    user_id,
    bao_id: Number(orderAll.value.bao_id),
    fx_user_id: Number(orderAll.value.fx_user_id),
    team_id: Number(orderAll.value.team_id),
  };
  toPublish('store/addTeamBaoOrder', payload, (obj: any) => {
    const { code, msg, data } = obj;
    if (code === 1) {
      repeat.value = true;
      redirectTo('groupBenefits/placeOrder?id=' + data.order_id);
    } else if (code === 0) {
      repeat.value = true;
      openBankPad();
    } else {
      repeat.value = true;
      errorToast(msg);
    }
  });
}
function timeup() {
  if (orderID.value) {
    init();
  }
}
/**
 * 团惠包出现商品确实等出现弹框再次确认购买
 * @param user_id  用户ID
 */
function BuyAgain() {
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    user_id,
  };
  toPublish('store/clearTeamCart', payload, (obj: any) => {
    const { code, msg } = obj;
    closeBankPad();
    if (code === 1) {
      repeat.value = true;
      addGroup();
    } else {
      repeat.value = true;
      errorToast(msg);
    }
  });
}
/**
 * 新建团惠包商品
 * @param user_id  用户ID
 * @param bao_id  团惠包ID
 */
function addGroup() {
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    user_id,
    bao_id: Number(orderAll.value.bao_id),
  };
  toPublish('store/addTeamCart', payload, (obj: any) => {
    const { code, msg } = obj;
    if (code === 1) {
      repeat.value = true;
      successToast(msg);
      redirectTo('groupBenefits/groupEditor');
    } else {
      repeat.value = true;
      errorToast(msg);
    }
  });
}
/**
 * 打开新建团惠包提示框
 */
function openBankPad() {
  PobulicePopup.value.open();
}
/**
 * 关闭新建团惠包提示框
 */
function closeBankPad() {
  PobulicePopup.value.close();
}
</script>

<style scoped lang="less">
.packageLsit {
  width: 100%;
  padding: 0px 20rpx;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  image {
    width: 50rpx;
    height: 50rpx;
  }
  text {
    font-size: 30rpx;
    font-weight: 400;
    color: #181818;
    margin: 0px 10rpx;
  }
}
</style>